<template>
  <div>
    <div style="display: flex;justify-content: space-between">
    </div>
    <div style="margin-top: 10px">
      <!-- 列表 -->
      <el-table v-loading="loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                size="mini"
                :data="emps"
                stripe
                border
                style="width: 100%">

        <el-table-column prop="emName"
                         label="姓名"
                         fixed
                         width="60">
        </el-table-column>
        <el-table-column prop="emGender"
                         width="50"
                         label="性别">
        </el-table-column>
        <el-table-column prop="emBirthday"
                         width="130"
                         align="left"
                         label="出生日期">
        </el-table-column>
        <!-- emEmpFace员工二寸图片 -->

        <!-- emIdCardType身份证类型 -->
        <el-table-column prop="emIdCard"
                         width="150"
                         align="left"
                         label="身份证号">
        </el-table-column>
        <el-table-column prop="emWedlock"
                         width="75"
                         label="婚姻状况">
        </el-table-column>
        <el-table-column prop="emNation"
                         width="75"
                         label="民族">
        </el-table-column>
        <el-table-column prop="emBloodType"
                         width="75"
                         label="血型">
        </el-table-column>
        <el-table-column prop="emNativePlace"
                         width="90"
                         label="籍贯">
        </el-table-column>
        <el-table-column prop="emPosition"
                         width="90"
                         align="left"
                         label="政治面貌">
        </el-table-column>
        <el-table-column prop="emEmail"
                         width="160"
                         align="left"
                         label="电子邮件">
        </el-table-column>
        <el-table-column prop="emPhone"
                         width="120"
                         align="left"
                         label="电话号码">
        </el-table-column>
        <el-table-column prop="emAddress"
                         width="180"
                         align="left"
                         label="联系地址">
        </el-table-column>
        <el-table-column prop="emDucation"
                         width="120"
                         align="left"
                         label="学历">
        </el-table-column>
        <el-table-column prop="emSpeciaty"
                         width="110"
                         align="left"
                         label="专业">
        </el-table-column>
        <el-table-column prop="emSchool"
                         width="110"
                         align="left"
                         label="学校">
        </el-table-column>
        <el-table-column fixed="right"
                         width="200"
                         label="操作">
          <template slot-scope="scope">
            <el-button @click="showEditEmpView(scope.row)"
                       style="padding: 3px"
                       size="mini">分配职位</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页判断 -->
      <div style="display: flex;justify-content: flex-end">
        <el-pagination background
                       @current-change="currentChange"
                       @size-change="sizeChange"
                       :page-size="size"
                       layout='sizes,prev, pager, next, jumper, ->, total'
                       :total="total">
        </el-pagination>
      </div>

    </div>

    <!-- 添加员工信息 -->
    <el-dialog :title="title"
               :visible.sync="dialogVisible"
               width="80%">
      <div>
        <el-form :model="emp"
                 :rules="rules"
                 ref="empForm">
          <el-row>
            <el-col :span="6">
              <el-form-item label="姓名:"
                            prop="name">
                <el-input size="mini"
                          style="width: 150px"
                          prefix-icon="el-icon-edit"
                          v-model="emp.name"
                          placeholder="请输入员工姓名"
                          disabled>
                          ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="性别:"
                            prop="gender">
                <el-radio-group v-model="emp.gender"
                                style="margin-top: 8px"
                                disabled>
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="出生日期:"
                            prop="birthday">
                <el-date-picker v-model="emp.birthday"
                                size="mini"
                                type="date"
                                value-format="yyyy-MM-dd"
                                style="width: 150px;"
                                placeholder="出生日期"
                                disabled
                                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="政治面貌:"
                            prop="politicId">
                <el-select v-model="emp.politicsStatus.name"
                           placeholder="政治面貌"
                           @change="getPolitic"
                           size="mini"
                           style="width: 200px;"
                           disabled>
                  <el-option v-for="item in politicsstatus"
                             :key="item.id"
                             :label="item.name"
                             :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="民族:"
                            prop="nationId">
                <el-select v-model="emp.nation.name"
                          @change="getNation"
                           placeholder="民族"
                           size="mini"
                           style="width: 150px;"
                           disabled>
                  <el-option v-for="item in nations"
                             :key="item.id"
                             :label="item.name"
                             :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="籍贯:"
                            prop="nativePlaceId">
                <el-input size="mini"
                          style="width: 120px"
                          prefix-icon="el-icon-edit"
                          v-model="emp.nativePlaceId"
                          placeholder="请输入籍贯"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="电子邮箱:"
                            prop="email">
                <el-input size="mini"
                          style="width: 150px"
                          prefix-icon="el-icon-message"
                          v-model="emp.email"
                          placeholder="请输入电子邮箱"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="联系地址:"
                            prop="address">
                <el-input size="mini"
                          style="width: 200px"
                          prefix-icon="el-icon-edit"
                          v-model="emp.address"
                          placeholder="请输入联系地址"
                          disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="职位:"
                            prop="posId">
                <el-select v-model="emp.position.positionName"
                          @change="getPos"
                           placeholder="职位"
                           size="mini"
                           style="width: 150px;">
                  <el-option v-for="item in positions"
                             :key="item.id"
                             :label="item.positionName"
                             :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="所属部门:"
                            prop="departmentId">
                <el-popover placement="right"
                            title="请选择部门"
                            width="200"
                            trigger="manual"
                            v-model="popVisible">
                  <el-tree default-expand-all
                           :data="allDeps"
                           :props="defaultProps"
                           :expand-on-click-node="false"
                           @node-click="handleNodeClick"></el-tree>
                  <div slot="reference"
                       style="width: 150px;display: inline-flex;font-size: 13px;border: 1px solid #dedede;height: 26px;border-radius: 5px;cursor: pointer;align-items: center;padding-left: 8px;box-sizing: border-box"
                       @click="showDepView">{{emp.department.organizationName}}
                  </div>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="电话号码:"
                            prop="phone">
                <el-input size="mini"
                          style="width: 200px"
                          prefix-icon="el-icon-phone"
                          v-model="emp.phone"
                          placeholder="电话号码"
                          disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="工号:"
                            prop="workID">
                <el-input size="mini"
                          style="width: 150px"
                          prefix-icon="el-icon-edit"
                          v-model="emp.workID"
                          placeholder="工号"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="学历:"
                            prop="tiptopDegree">
                <el-select v-model="emp.tiptopDegree"
                          @change="getTiptopDegree"
                           placeholder="学历"
                           size="mini"
                           style="width: 150px;"
                           disabled>
                  <el-option v-for="item in tiptopDegrees"
                             :key="item"
                             :label="item.name"
                             :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="毕业院校:"
                            prop="school">
                <el-input size="mini"
                          style="width: 150px"
                          prefix-icon="el-icon-edit"
                          v-model="emp.school"
                          placeholder="毕业院校名称"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="专业名称:"
                            prop="specialty">
                <el-input size="mini"
                          style="width: 200px"
                          prefix-icon="el-icon-edit"
                          v-model="emp.specialty"
                          placeholder="请输入专业名称"
                          disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="入职日期:"
                            prop="entryDate">
                <el-date-picker v-model="emp.entryDate"
                                size="mini"
                                type="date"
                                style="width: 130px;"
                                placeholder="入职日期"
                                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="身份证号码:"
                            prop="idCard">
                <el-input size="mini"
                          style="width: 180px"
                          prefix-icon="el-icon-edit"
                          v-model="emp.idCard"
                          placeholder="请输入身份证号码"
                          disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="婚姻状况:"
                            prop="wedlock">
                <el-radio-group v-model="emp.wedlock"
                                style="margin-top: 8px"
                                disabled>
                  <el-radio label="已婚">已婚</el-radio>
                  <el-radio label="未婚">未婚</el-radio>
                  <el-radio label="离异">离异</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary"
                   @click="doAddEmp">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'EmpBasic',
  data() {
    return {
      title: '',
      popVisible2: false,
      showAdvanceSearchView: false,
      allDeps: [],
      nations: [],
      politicsstatus: [],
      positions: [],
      tiptopDegrees: [],
      emp: {
        ids: '',
        department: {organizationName: "请选择"},
        education: {name: ""},
        position: {positionName: ""},
        politicsStatus: {name: ""},
        nation: {name: ""},
        name: '',
        gender: '',
        birthday: '',
        idCard: '',
        wedlock: '',
        nationId: '',
        nativePlaceId: '',
        politicId: '',
        email: '',
        phone: '',
        address: '',
        departmentId: '',
        jobLevelId: '',
        posId: '',
        engageForm: '',
        tiptopDegree: '',
        specialty: '',
        school: '',
        beginDate: '',
        workState: '',
        workID: '',
        contractTerm: '',
        conversionTime: '',
        notWorkDate: '',
        beginContract: '',
        endContract: '',
        workAge: '',
        salaryId: '',
        salary: '',
      },
      popVisible: false,
      dialogVisible: false,
      emps: [],
      loading: false,
      total: 0,
      currentPage: 1,
      size: 10,
      defaultProps: {
        children: 'children',
        label: 'organizationName',
      },
      rules: {
        departmentId: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
        ],
        posId: [{ required: true, message: '请输入职位', trigger: 'blur' }],
        entryDate: [
          { required: true, message: '请输入入职日期', trigger: 'blur' },
        ],
  },
  } 
  },
  mounted() {
    this.initEmps()
    this.initData()
  },

  methods: {
     getProjectNum () {
      const projectTime = new Date() // 当前中国标准时间
      const Year = projectTime.getFullYear() // 获取当前年份 支持IE和火狐浏览器.
      const Month = projectTime.getMonth() + 1 // 获取中国区月份
      const Day = projectTime.getDate() // 获取几号
      var CurrentDate = Year
      if (Month >= 10) { // 判断月份和几号是否大于10或者小于10
        CurrentDate += Month
      } else {
        CurrentDate += '0' + Month
      }
      if (Day >= 10) {
        CurrentDate += Day
      } else {
        CurrentDate += '0' + Day
      }
      return CurrentDate
    },
    getTiptopDegree(key){
      this.emp.tiptopDegree=key
    },
    getPos(key){
      this.emp.posId=key
    },
    getNation(key){
      this.emp.nationId=key
    },
    getPolitic(key){
        this.emp.politicId=key
    },
    showEditEmpView(data) {
      this.title = '编辑员工信息'
      this.emp = {
        ids: data.id,
        department: {organizationName: "请选择"},
        position: {positionName: ""},
        politicsStatus: {name: data.emPosition.substring(data.emPosition.indexOf('-')+1,data.emPosition.length)},
        nation: {name: data.emNation.substring(data.emNation.indexOf('-')+1,data.emNation.length)},
        name: data.emName,
        gender: data.emGender,
        birthday: data.emBirthday,
        idCard: data.emIdCard,
        empFace: data.emEmpFace,
        wedlock: data.emWedlock,
        nationId: data.emNation.substring(0,data.emNation.indexOf('-')),
        nativePlaceId: data.emNativePlace,
        politicId:  data.emPosition.substring(0,data.emPosition.indexOf('-')),
        email: data.emEmail,
        phone: data.emPhone,
        address: data.emAddress,
        bloodType: data.emBloodType,
        departmentId: '',
        posId: '',
        entryDate: '',
        workCity: data.emWorkCity,
        tiptopDegree: data.emDucation,
        specialty: data.emSpeciaty,
        school: data.emSchool,
        workID: "PG"+this.getProjectNum() + Math.floor(Math.random() * 10000),
        }
      this.dialogVisible = true
    },
    initData() {
      if (!window.sessionStorage.getItem('nations')) {
        this.getRequest('/emp/nation').then((resp) => {
          if (resp) {
            this.nations = resp
            window.sessionStorage.setItem('nations', JSON.stringify(resp))
          }
        })
      } else {
        this.nations = JSON.parse(window.sessionStorage.getItem('nations'))
      }
      
      if (!window.sessionStorage.getItem('politicsstatus')) {
        this.getRequest('/politics-status/').then((resp) => {
          if (resp) {
            this.politicsstatus = resp
            window.sessionStorage.setItem(
              'politicsstatus',
              JSON.stringify(resp)
            )
          }
        })
      } else {
        this.politicsstatus = JSON.parse(
          window.sessionStorage.getItem('politicsstatus')
        )
      }
      if (!window.sessionStorage.getItem('deps')) {
        this.getRequest('/org/dep/allDep').then((resp) => {
          if (resp) {
            this.allDeps = resp
            window.sessionStorage.setItem('deps', JSON.stringify(resp))
          }
        })
      } else {
        this.allDeps = JSON.parse(window.sessionStorage.getItem('deps'))
      }

      if (!window.sessionStorage.getItem('positions')) {
        this.getRequest('/organization/sequence/allPos').then((resp) => {
          if (resp) {
            console.log('AAA', resp)
            this.positions = resp
            window.sessionStorage.setItem('positions', JSON.stringify(resp))
          }
        })
      } else {
        this.positions = JSON.parse(window.sessionStorage.getItem('positions'))
      }
    },
    showDepView() {
      this.popVisible = !this.popVisible
    },
    doAddEmp() {
      console.log(this.emp,"111111111111")
        this.$refs['empForm'].validate((valid) => {
          if (valid) {
            this.postRequest('/employee-base/', this.emp).then((resp) => {
              if (resp) {
                this.deleteRequest('/employee-base/es/employ/'+this.emp.ids).then(()=>{})
                this.dialogVisible = false
                this.initEmps()
              }
            })
          }
        })
    },
    handleNodeClick(data) {
      console.log('3333', data)
      this.emp.department.organizationName = data.organizationName
      this.emp.departmentId = data.id
      this.popVisible = !this.popVisible
    },
    sizeChange(size) {
      this.size = size
      this.initEmps()
    },

    currentChange(currentPage) {
      this.currentPage = currentPage
      this.initEmps()
    },

    initEmps(type) {
      this.loading = true
      let url = '/employee-base/es/employ/' + this.currentPage + '/' + this.size
      this.getRequest(url).then((resp) => {
        this.loading = false
        if (resp) {
          this.emps = resp
        }
      })
    },
    }
    
   
    }
</script>

<style scoped>
</style>